import { h } from 'vue';
import { Avatar, Tag, Tooltip, Image } from 'ant-design-vue';
import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import {JVxeTypes,JVxeColumn} from '/@/components/jeecg/JVxeTable/types'
// 假设 now 是当前时间，createdAt 是要比较的创建时间
function isMoreThan24HoursAgo(now: Date, createdAt: Date): boolean {
    const serviceTime = createdAt.getTime()+(24 * 60 * 60 * 1000); // 1天的毫秒数
	const finishTime = now.getTime();
    return finishTime <= serviceTime;
}
//列表数据
export const columns: BasicColumn[] = [
    /*{
        title: '车牌号',
        align:"center",
        dataIndex: 'licensePlate'
    },*/
	{
        title: '客户名称',
        align:"center",
        dataIndex: 'customerName'
    },
    {
        title: '工单描述',
        align:"center",
        dataIndex: 'description'
    },
    {
        title: '服务人员',
		helpMessage: ['安装或维护师傅'],
        align:"center",
        dataIndex: 'serviceStaff_dictText'
    },
   {
    title: '创建时间',
    align:"center",
    sorter: true,
    dataIndex: 'createTime'
   },
   {
    title: '工单类型',
    align:"center",
    dataIndex: 'workOrderType_dictText',
	customRender: ({ text, record, index }) => {
		let serviceTime = new Date(record.serviceTime);
		let finishTime = new Date();
		if(record.finishTime){
			finishTime = new Date(record.finishTime);
		}
		const enable = isMoreThan24HoursAgo(finishTime, serviceTime);
		const color = enable ? 'green' : 'red';
		//return h(Tag, { color: color }, () => text);
		return h(Tag, {color: '#fff',style: {
		backgroundColor: color,
		} }, () => text);
		//return h('span', 'ppppp');
    },
   },
   {
    title: '工单状态',
    align:"center",
    dataIndex: 'workOrderStatus_dictText'
   },
  {
    title: '创建人',
    align:"center",
    dataIndex: 'createBy'
  },
   {
    title: '联系人',
    align:"center",
    dataIndex: 'contacts'
   },
   {
    title: '联系电话',
    align:"center",
    dataIndex: 'telephone'
   },
   {
    title: '上门地址',
    align:"center",
    dataIndex: 'homeAddress',
   },
   {
    title: '详细地址',
    align:"center",
    dataIndex: 'address'
   },
   {
    title: '预约时间',
    align:"center",
    dataIndex: 'appointmentTime'
   },
  {
    title: '预约结果',
    align:"center",
    dataIndex: 'appointmentResults'
  },
   {
    title: '服务时间',
    align:"center",
    sorter: true,
    dataIndex: 'serviceTime'
   },
   {
    title: '结算状态',
    align:"center",
    dataIndex: 'settlementStatus_dictText'
   },
   {
    title: '完成时间',
    align:"center",
    sorter: true,
    dataIndex: 'finishTime'
   },
   {
    title: '优先级',
    align:"center",
    sorter: true,
    dataIndex: 'priority_dictText'
   },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
	// {
  //   title: "关键字",
  //     field: 'licensePlate',
  //     component: 'Input',
  //   componentProps: {
  //     placeholder: '请输入车牌号/客户名称/客户电话/联系人名称/联系人电话/工单地址查询',
  //     // style: { width: '800px'},
  //   },
  //     // colProps: {offset: 3},
 	// },

	/*{
      label: "车牌号",
      field: 'licensePlate',
      component: 'Input',
      //colProps: {span: 6},
 	},*/
  {
    label: "客户名称",
    field: 'customerName',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: "联系人",
    field: 'contacts',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
      label: "联系电话",
      field: 'telephone',
      component: 'Input',
      // colProps: {span: 8},
  },
	// {
  //     label: "工单状态",
  //     field: 'workOrderStatus',
  //     component: 'JDictSelectTag',
  //     componentProps:{
  //         dictCode:"workOrderStatus"
  //     },
  //     //colProps: {span: 6},
 	// },
	// {
  //     label: "结算状态",
  //     field: 'settlementStatus',
  //     component: 'JDictSelectTag',
  //     componentProps:{
  //         dictCode:"settlementstatus"
  //     },
  //     //colProps: {span: 6},
 	// },
	// {
  //     label: "是否免费",
  //     field: 'free',
  //     component: 'JDictSelectTag',
  //     componentProps:{
  //         dictCode:"free"
  //     },
  //     //colProps: {span: 6},
 	// },
	// {
  //     label: "服务类型",
  //     field: 'serviceType',
  //     component: 'JDictSelectTag',
  //     componentProps:{
  //         dictCode:"serviceType"
  //     },
  //     //colProps: {span: 6},
 	// },
	// {
  //     label: "优先级",
  //     field: 'priority',
  //     component: 'JDictSelectTag',
  //     componentProps:{
  //         dictCode:"workOrderPriority"
  //     },
  //     //colProps: {span: 6},
 	// },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '工单类型',
    field: 'workOrderType',
	defaultValue: "2",
	ifShow:false,
    component: 'JDictSelectTag',
    componentProps:{
        dictCode:"workOrderType"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入工单类型!'},
          ];
     },
  },
  {
    label: '选择客户信息',
    field: 'popup',
    component: 'JPopup',
	helpMessage: ['选择会修改客户信息，不选择会新增客户信息'],
    componentProps: ({ formActionType }) => {
      const {setFieldsValue} = formActionType;
      return{
        setFieldsValue:setFieldsValue,
        //online报表编码
        code:"crmCustomerinstallInfo",
        //是否为多选
        multi:false,
        //字段配置
        fieldConfig: [
          { source: 'id', target: 'customerId' },
		  { source: 'contact_number', target: 'telephone' },
          { source: 'customer_name', target: 'customerName' },
		  { source: 'contacts', target: 'contacts' },
		  { source: 'address', target: 'address' },
		  { source: 'home_address', target: 'homeAddress' },
        ],
      }
    },
  },
  {
    label: '客户ID',
    field: 'customerId',
    component: 'Input',
    show: false
  },
  {
    label: '客户名称',
	helpMessage: ['选择会修改客户信息，不选择会新增客户信息'],
    field: 'customerName',
    component: 'Input',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入客户名称!'},
          ];
     },
  },
  {
    label: '联系人',
	helpMessage: ['选择会修改客户信息，不选择会新增客户信息'],
    field: 'contacts',
    component: 'Input',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入联系人!'},
          ];
     },
  },
  {
    label: '联系电话',
	helpMessage: ['选择会修改客户信息，不选择会新增客户信息'],
    field: 'telephone',
    component: 'Input',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入联系电话!'},
                 { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
          ];
     },
  },
  {
    label: '上门地址',
    field: 'homeAddress',
	helpMessage: ['选择会修改客户信息，不选择会新增客户信息'],
    component: 'JAreaLinkage',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请选择上门地址!'},
          ];
     },
	 defaultValue: ['520000','520100','520101'],
  },
  {
    label: '详细地址',
	helpMessage: ['选择会修改客户信息，不选择会新增客户信息'],
    field: 'address',
    component: 'Input',
	dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入详细地址!'},
          ];
     },
  },
  /*{
    label: '选择车牌号',
    field: '2licensePlate2',
    component: 'JPopup',
    componentProps: ({ formActionType }) => {
      const {setFieldsValue} = formActionType;
      return{
        setFieldsValue:setFieldsValue,
        //online报表编码
        code:"vehicle_info",
        //是否为多选
        multi:false,
        //字段配置
        fieldConfig: [
          { source: 'license_plate_number', target: 'licensePlate' },
        ],
      }
    },
  },
    {
        label: '车牌号',
        field: 'licensePlate',
        component: 'Input',
        dynamicRules: ({model,schema}) => {
            return [
                { required: true, message: '请输入或选择车牌号!'},
            ];
        },
    },*/
  {
    label: '工单描述',
    field: 'description',
    component: 'InputTextArea',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入工单描述!'},
          ];
     },
  },
  {
    label: '预约时间',
    field: 'appointmentTime',
    component: 'DatePicker',
	ifShow:false,
    componentProps: {
       showTime:true,
       valueFormat: 'YYYY-MM-DD HH:mm:ss'
     },
  },
  {
    label: '服务时间',
    field: 'serviceTime',
    component: 'DatePicker',
    componentProps: {
       showTime:true,
       valueFormat: 'YYYY-MM-DD HH:mm:ss'
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入服务时间!'},
          ];
     },
  },
  {
    label: '选择服务人员',
    field: 'serviceStaff',
    component: 'JSelectUser',
	helpMessage: ['选择安装或维护师傅'],
    componentProps:{
        labelKey:'realname',
        isRadioSelection:true
     },
	 dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '选择服务人员!'},
          ];
     },

  },
    {
        field: 'serviceStaff',
        helpMessage: ['手工输入服务人员的登录账号或者点击上面的选择用户'],
        component: 'Input',
		show: false,
        label: '直接输入服务人员',
        // colProps: { span: 12 },
    },
	// TODO 主键隐藏字段，目前写死为ID
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];
//子表单数据
//子表列表数据
export const maintainOrdersColumns: BasicColumn[] = [
   {
    title: '公司名称/个人',
    align:"center",
    dataIndex: 'companyNameIndividual'
   },
   {
    title: '故障类型',
    align:"center",
    dataIndex: 'faultType_dictText'
   },
   {
    title: '图片描述',
    align:"center",
    dataIndex: 'pictureDescription',
    customRender:render.renderImage,
   },
   {
    title: '视频描述',
    align:"center",
    dataIndex: 'videoDescription',
   },
   {
    title: '批量报修',
    align:"center",
    dataIndex: 'batchRepairReporting',
   },
];
export const maintainOrdersFormSchema: FormSchema[] = [
  {
    label: '公司名称/个人',
    field: 'companyNameIndividual',
    component: 'Input',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入公司名称/个人!'},
          ];
     },
  },
  {
    label: '故障类型',
    field: 'faultType',
    component: 'JSelectMultiple',
    componentProps:{
        dictCode:"crmFaultType"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入故障类型!'},
          ];
     },
  },
  {
    label: '图片描述',
    field: 'pictureDescription',
     component: 'JImageUpload',
     componentProps:{
      },
  },
  {
    label: '视频描述',
    field: 'videoDescription',
    component: 'JUpload',
    componentProps:{
     },
  },
  {
    label: '批量报修',
    field: 'batchRepairReporting',
    component: 'JUpload',
    componentProps:{
     },
  },
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];
//子表列表数据
export const installationOrderColumns: BasicColumn[] = [
   {
    title: '业务员',
    align:"center",
    dataIndex: 'salesman'
   },
   {
    title: '业户名称',
    align:"center",
    dataIndex: 'nameBusinessOwner'
   },
   {
    title: '安装设备',
    align:"center",
    dataIndex: 'installingEquipment_dictText'
   },
];
export const installationOrderFormSchema: FormSchema[] = [
  {
    label: '业务员',
    field: 'salesman',
    component: 'Input',
  },
  {
    label: '业户名称',
    field: 'nameBusinessOwner',
    component: 'Input',
  },
  {
    label: '行驶证照片',
    field: 'photoDrivingLicense',
     component: 'JImageUpload',
     componentProps:{
      },
	  show: false
  },
  {
    label: '车辆类型',
    field: 'vehicleType',
    component: 'JDictSelectTag',
    componentProps:{
        dictCode:"vehicleType"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入车辆类型!'},
          ];
     },
	 show: false
  },
  {
    label: '安装设备',
    field: 'installingEquipment',
    component: 'JDictSelectTag',
    componentProps:{
        dictCode:"crmInstallingEquipment"
     },
	 dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请选择安装设备!'},
          ];
     },
  },
  {
    label: '批量安装',
    field: 'batchInstallation',
    component: 'JUpload',
    componentProps:{
     },
	 show: false
  },
  {
    label: '接入说明',
    field: 'accessInstructions',
    component: 'JCheckbox',
    componentProps:{
        dictCode:"accessInstructions"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入接入说明!'},
          ];
     },
	 show: false
  },
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];
//子表列表数据
export const workOrderProcessColumns: BasicColumn[] = [
   {
    title: '创建日期',
    align:"center",
    sorter: true,
    dataIndex: 'createTime'
   },
   {
    title: '流程状态',
    align:"center",
    dataIndex: 'status_dictText'
   },
   {
    title: '流程内容',
    align:"center",
    dataIndex: 'content'
   },
];
//子表表格配置
export const workOrderProcessJVxeColumns: JVxeColumn[] = [
    {
      title: '创建日期',
      key: 'createTime',
      type: JVxeTypes.datetime,
      width:"200px",
      placeholder: '请输入${title}',
      defaultValue:'',
    },
    {
      title: '流程状态',
      key: 'status',
      type: JVxeTypes.inputNumber,
      width:"200px",
      placeholder: '请输入${title}',
      defaultValue:'',
    },
    {
      title: '流程内容',
      key: 'content',
      type: JVxeTypes.input,
      width:"200px",
      placeholder: '请输入${title}',
      defaultValue:'',
    },
  ]

// 高级查询数据
export const superQuerySchema = {
  createTime: {title: '提交时间',order: 0,view: 'datetime', type: 'string',},
  workOrderType: {title: '工单类型',order: 1,view: 'list', type: 'string',dictCode: 'workOrderType',},
  customerName: {title: '客户名称',order: 2,view: 'text', type: 'string',},
  contacts: {title: '联系人',order: 3,view: 'text', type: 'string',},
  telephone: {title: '联系电话',order: 4,view: 'text', type: 'string',},
  homeAddress: {title: '上门地址',order: 5,view: 'pca', type: 'string',},
  address: {title: '详细地址',order: 6,view: 'text', type: 'string',},
  licensePlate: {title: '车牌号',order: 7,view: 'text', type: 'string',},
  description: {title: '工单描述',order: 8,view: 'textarea', type: 'string',},
  appointmentTime: {title: '预约时间',order: 9,view: 'datetime', type: 'string',},
  serviceTime: {title: '服务时间',order: 10,view: 'datetime', type: 'string',},
  workOrderStatus: {title: '工单状态',order: 11,view: 'number', type: 'number',dictCode: 'workOrderStatus',},
  settlementStatus: {title: '结算状态',order: 12,view: 'number', type: 'number',dictCode: 'settlementstatus',},
  finishTime: {title: '完成时间',order: 15,view: 'datetime', type: 'string',},
  priority: {title: '优先级',order: 16,view: 'number', type: 'number',dictCode: 'workOrderPriority',},
  serviceStaff: {title: '服务人员',order: 17,view: 'sel_user', type: 'string',},
  //子表高级查询
  maintainOrders: {
    title: '维护订单',
    view: 'table',
    fields: {
        companyNameIndividual: {title: '公司名称/个人',order: 0,view: 'text', type: 'string',},
        faultType: {title: '故障类型',order: 1,view: 'list_multi', type: 'string',dictCode: 'faultType',},
        pictureDescription: {title: '图片描述',order: 2,view: 'image', type: 'string',},
        videoDescription: {title: '视频描述',order: 3,view: 'file', type: 'string',},
        batchRepairReporting: {title: '批量报修',order: 4,view: 'file', type: 'string',},
    }
  },
  installationOrder: {
    title: '安装订单',
    view: 'table',
    fields: {
        salesman: {title: '业务员',order: 0,view: 'text', type: 'string',},
        nameBusinessOwner: {title: '业户名称',order: 1,view: 'text', type: 'string',},
        photoDrivingLicense: {title: '行驶证照片',order: 2,view: 'image', type: 'string',},
        vehicleType: {title: '车辆类型',order: 3,view: 'list', type: 'string',dictCode: 'vehicleType',},
        installingEquipment: {title: '安装设备',order: 4,view: 'list', type: 'string',dictCode: 'crmInstallingEquipment',},
        batchInstallation: {title: '批量安装',order: 5,view: 'file', type: 'string',},
        accessInstructions: {title: '接入说明',order: 6,view: 'checkbox', type: 'string',dictCode: 'accessInstructions',},
    }
  },
  workOrderProcess: {
    title: '工单流程',
    view: 'table',
    fields: {
        createTime: {title: '创建日期',order: 0,view: 'datetime', type: 'string',},
        status: {title: '流程状态',order: 1,view: 'number', type: 'number',dictCode: 'processStatus',},
        content: {title: '流程内容',order: 2,view: 'text', type: 'string',},
    }
  },
};

/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[]{
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
